package Entity;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import Control.MySQLController;

public class Workshop_ReportEntity {
	
	private Calendar date = Calendar.getInstance();
	private int totalQuantity;
	private double totalSales;
	private int[] quantity = new int[4];
	private double[] sales = new double[4];
	private ArrayList<Shop_Product> catProds = new ArrayList<Shop_Product>();
	private ArrayList<Integer> prodQuantity = new ArrayList<Integer>();
	private ArrayList<ArrayList<Shop_Customization>> prodCustomizations = new ArrayList<ArrayList<Shop_Customization>>();
	
	public Calendar getDate() {
		return date;
	}
	public void setDate(Calendar date) {
		this.date = date;
	}
	public int getTotalQuantity() {
		return totalQuantity;
	}
	public void setTotalQuantity(int totalQuantity) {
		this.totalQuantity = totalQuantity;
	}
	public double getTotalSales() {
		return totalSales;
	}
	public void setTotalSales(double totalSales) {
		this.totalSales = totalSales;
	}
	public int[] getQuantity() {
		return quantity;
	}
	public void setQuantity(int[] quantity) {
		this.quantity = quantity;
	}
	public double[] getSales() {
		return sales;
	}
	public void setSales(double[] sales) {
		this.sales = sales;
	}
	public ArrayList<Shop_Product> getCatProds() {
		return catProds;
	}
	public void setCatProds(ArrayList<Shop_Product> catProds) {
		this.catProds = catProds;
	}
	public ArrayList<Integer> getProdQuantity() {
		return prodQuantity;
	}
	public void setProdQuantity(ArrayList<Integer> prodQuantity) {
		this.prodQuantity = prodQuantity;
	}
	public ArrayList<ArrayList<Shop_Customization>> getProdCustomizations() {
		return prodCustomizations;
	}
	public void setProdCustomizations(
			ArrayList<ArrayList<Shop_Customization>> prodCustomizations) {
		this.prodCustomizations = prodCustomizations;
	}
	
	public void getReportDetails(){
		retrieveTotal();
	}
	
	public int retrieveTotal(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		MySQLController db = new MySQLController();
		ResultSet rs = null;
		db.getConnection();
		String sql = "SELECT SUM(quantity) AS totalquantity, (SUM(quantity) * unit_price) AS totalprice, category.category_code FROM category ";
		sql += "INNER JOIN product ON category.category_code = product.workshop_id ";
		sql += "INNER JOIN customization ON product.product_id = customization.product_id ";
		sql += "INNER JOIN order_form ON customization.order_id = order_form.order_id ";
		sql += "WHERE MONTH(order_date) = MONTH('" + sdf.format(date.getTime()) + "') AND YEAR(order_date) = YEAR('" + sdf.format(date.getTime()) + "') ";
		sql += "GROUP BY category.category_code ";
		sql += "ORDER BY category.category_code";
		rs = db.readRequest(sql);
		try{
			int i = 0;
			totalQuantity = 0;
			totalSales = 0;
			
			for(int index = 0; index < 4; index++){
				quantity[index] = 0;
				sales[index] = 0;
			}
			while(rs.next()){
				String category = rs.getString("category_code");
				if(category.equals("DEC")){
					i = 0;
				}
				else if(category.equals("HAN")){
					i = 1;
				}
				else if(category.equals("POT")){
					i = 2;
				}
				else{
					i = 3;
				}
				quantity[i] = rs.getInt("totalquantity");
				sales[i] = rs.getDouble("totalprice");
				totalQuantity += quantity[i];
				totalSales += sales[i];
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return totalQuantity;
	}
	
	public void retrieveProdsUnderCat(String catId, int month){
		prodQuantity = new ArrayList<Integer>();
		prodCustomizations = new ArrayList<ArrayList<Shop_Customization>>();
		setCatProds(Shop_Product.getProductsUnderCategory(catId));
		for(int i = 0; i < catProds.size(); i++){
			prodCustomizations.add(Shop_Customization.retrieveProdCustomizations(catProds.get(i).getProductId(), month));

			int custQuan = 0;
			for(int c = 0; c < prodCustomizations.get(i).size(); c++){
				custQuan += prodCustomizations.get(i).get(c).getQuantity();
			}
			prodQuantity.add(custQuan);
		}
	}
}
